<template>
    <div class='approval'>
      <el-card class="box-card" style="margin-top: 110px;">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class='menu-search'> 
            <div class='menu-title'>
              <!-- 关键词 -->
             <div class='searchItem'>
                <span>{{$t('RiskManager.KeyWord')}}</span>
                <el-select v-model='listQuery.keyWords' size='small'>
                  <el-option
                  v-for='item in keywordArr'
                  :key='item.itemCode'
                  :label='item.desc'
                  :value='item.itemCode'>
                  </el-option>
                </el-select>
              </div>
              <!-- 类别 -->
              <div class='searchItem'>
                <span>{{$t('approvalDetail.type')}}</span>
                <el-select v-model='listQuery.modelType' size='small'>
                  <el-option
                  v-for='item in typeArr'
                  :key='item.compCode'
                  :label='item.desc'
                  :value='item.compCode'>
                  </el-option>
                </el-select>
              </div>
              <!-- APP渠道 -->
              <div class='searchItem'>
                <span>{{$t('ElectricChannel.AppChannel')}}</span>
                <el-select v-model='listQuery.channel' size='small'>
                    <el-option
                    v-for='item in appChannel'
                    :key='item.channel'
                    :label='item.org_name'
                    :value='item.channel'>
                    </el-option>
                </el-select>
                </div>
            </div>
            <div class='menu-title-btn'>
                <button type='button' @click='search'>
                    {{$t('btn.query')}}
                </button>
                <button type='button' @click='reset'>
                    {{$t('btn.reset')}}
                </button>
            </div>
        </div>
      </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
          <div class='title'>
              {{$t('ElectricChannel.dataList')}}
          </div>
          <el-table
              v-loading="listLoading"
              ref='multipleTable'
              :data='tableData'
              tooltip-effect='dark'
              style='width: 100%; margin-top: 5px;'
              @selection-change='handleSelectionChange'
              border
              size='mini'>
              <el-table-column
                :label="$t('RiskManager.number')"
                header-align='center'
                align='center'
                width="80">
                <template slot-scope="scope">
                  <span>{{scope.row.index}}</span>
                </template>
              </el-table-column>
              <!-- APP名称 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='channel'
              width="150"
              :label="$t('appHelpCenter.appName')">
              </el-table-column>
              <!-- 类别 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='compCode'
              width="130"
              :label="$t('approvalDetail.type')">
              </el-table-column>
              <!-- 关键词 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='itemCode'
              width="230"
              :label="$t('RiskManager.KeyWord')">
              </el-table-column>
              <!-- 备注 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='desc'
              width="400"
              :label="$t('user.remarks')">
              </el-table-column>
              <!-- 状态 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='modelStatus'
              width="160"
              :label="$t('grouping.status')">
              <template slot-scope="scope">
                <el-radio-group v-model="scope.row.modelStatus" size="small" @change.native="changeStatus(scope.row)">
                    <el-radio :label="1" size="mini">{{$t('AppVersion.enable')}}</el-radio>
                    <el-radio :label="2" size="mini">{{$t('RiskManager.suspend')}}</el-radio>
                </el-radio-group>
              </template>
              </el-table-column>
              <el-table-column 
                align="center" 
                :label="$t('user.operation')" 
                width="100" 
                fixed="right"> 
                    <template slot-scope="scope">
                        <el-button size="small" type="text" @click="handleUpdate(scope.row)">{{$t('btn.edit')}}
                        </el-button>
                    </template> 
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listQuery.page"
            :page-sizes="[10, 30, 50, 100]"
            :page-size="listQuery.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
          </div>
        </el-card>
        <el-dialog :title="$t('route.ModelManager')" :visible.sync="dialogFormVisible">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <!-- 类别 -->
            <el-form-item :label="$t('approvalDetail.type')" prop="compCode">
              <el-select v-model="ruleForm.compCode" size='small' disabled="">
                <el-option
                v-for='item in typeArr'
                :key='item.compCode'
                :label='item.desc'
                :value='item.compCode'>
                </el-option>
              </el-select>
            </el-form-item>
            <!-- 关键词 -->
            <el-form-item :label="$t('RiskManager.KeyWord')" prop="itemCode">
              <el-select v-model="ruleForm.itemCode" size='small' disabled="">
                <el-option
                v-for='item in keywordArr'
                :key='item.itemCode'
                :label='item.desc'
                :value='item.itemCode'>
                </el-option>
              </el-select>
            </el-form-item>
            <!-- 阈值类型 -->
            <el-form-item :label="$t('RiskManager.thresholdType')" prop="paramType">
              <el-select v-model="ruleForm.paramType" size='small' @change="changeType(ruleForm.paramType)" disabled="">
                <el-option :label="$t('RiskManager.null')" value=""></el-option>
                <el-option :label="$t('RiskManager.numType')" value="1"></el-option>
                <el-option :label="$t('RiskManager.characterType')" value="2"></el-option>
              </el-select>
            </el-form-item>
            <div style="display: flex;padding: 0 0 15px 100px;box-sizing: border-box;" v-if="isType === '1'" key="1">
              <el-table
                :data="ruleForm.items"
                style="width: 100%;"
                border
                size="small">
                <el-table-column
                  header-align='center'
                  align='center'
                  :label="$t('RiskManager.threshold')">
                    <el-table-column
                    header-align='center'
                    align='center'
                    :label="$t('RiskManager.minVal')">
                      <template slot-scope="scope">
                        <el-form-item :prop="'items.' + scope.$index + '.min'" :rules="rules.min" label-width="0">
                          <el-input size='small' :placeholder="$t('rule.inp')" v-model.number="scope.row.min"></el-input>
                        </el-form-item>
                      </template>
                    </el-table-column>
                    <el-table-column
                    header-align='center'
                    align='center'
                    :label="$t('RiskManager.maxVal')">
                      <template slot-scope="scope">
                        <el-form-item :prop="'items.' + scope.$index + '.max'" :rules="rules.max" label-width="0">
                          <el-input size='small' :placeholder="$t('rule.inp')" v-model.number="scope.row.max"></el-input>
                        </el-form-item>
                      </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column
                  header-align='center'
                  align='center'
                  :label="$t('RiskManager.score')">
                  <template slot-scope="scope">
                    <el-form-item :prop="'items.' + scope.$index + '.score'" :rules="rules.score" label-width="0">
                      <el-input size='small' :placeholder="$t('rule.inp')" v-model.number="scope.row.score"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <div style="display: flex;padding: 0 0 15px 100px;box-sizing: border-box;" v-if="isType === '2'" key="2">
              <el-table
                :data="ruleForm.items"
                style="width: 100%;"
                border
                size="small">
                <el-table-column
                  header-align='center'
                  align='center'
                  :label="$t('RiskManager.threshold')">
                  <template slot-scope="scope">
                    <el-form-item :prop="'items.' + scope.$index + '.str'" :rules="rules.str" label-width="0">
                      <el-input size='small' :placeholder="$t('rule.inp')" v-model="scope.row.str"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column
                  header-align='center'
                  align='center'
                  :label="$t('RiskManager.score')">
                  <template slot-scope="scope">
                    <el-form-item :prop="'items.' + scope.$index + '.score'" :rules="rules.score" label-width="0">
                      <el-input size='small' :placeholder="$t('rule.inp')" v-model.number="scope.row.score"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <el-form-item :label="$t('user.remarks')" prop="remark" size='small'>
              <el-input type="textarea" :placeholder="$t('rule.toLength')" :maxlength="500" :autosize="{ minRows: 3, maxRows: 5}" v-model="ruleForm.remark" disabled=""></el-input>
            </el-form-item>
            <div style="text-align: right;">
              <el-form-item>
                <el-button @click="resetForm('ruleForm')">{{$t('btn.cancel')}}</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">{{$t('btn.confirm')}}</el-button>
              </el-form-item>
            </div>
          </el-form>
        </el-dialog>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getChannels } from "api/customQuery/index";
import { getDataItemList, getKeyWordsList, getModelShowPage, getDataChannelDetail, updateDataChannel, enableDataChannel } from 'api/RiskManager/ModelManager/index'
export default {
  data() {
    return {
      ruleForm: {
        compCode: '',
        itemCode: '',
        paramType: '',
        items: [],
        remark: ''
      },
      rules: {
        str: [
          { required: true, message: this.$t('RiskManager.ruleTshold')}
        ],
        min: [
          { required: true, message: this.$t('RiskManager.inpThresholdMin')}
        ],
        max: [
          { required: true, message: this.$t('RiskManager.inpThresholdMax')}
        ],
        score: [
          { required: true, message: this.$t('RiskManager.ruleScore')}
        ],
      },
      isType: '',
      dialogFormVisible: false,
      listLoading: false,
      total: null,
      expDis: true,
      listQuery: {
        limit: 10,
        page: 1,
        keyWords: '',
        channel: '',
        modelType: ''
      },
      tableData: [],
      multipleSelection: [],
      typeArr: [],
      appChannel: [],
      keywordArr: [],
      thresholdTypeArr: [
        {
          value: 1,
          label: this.$t("RiskManager.numType")
        },
        {
          value: 2,
          label: this.$t("RiskManager.characterType")
        }
      ],
      currentId: '',
      currentObj: {}
    };
  },
  computed: {
    ...mapGetters(["elements", "ModelManagerPage"])
  },
  created() {
    if (this.ModelManagerPage.has(this.$route.path)) {
      this.listQuery = this.ModelManagerPage.get(this.$route.path);
    }
    // 渠道下拉
    getChannels().then(res => {
      this.appChannel = res.data;
      this.listQuery.channel = this.appChannel[0].channel;
      this.getList();
    });
    // 类别
    getDataItemList()
    .then(res => {
      this.typeArr = res.data;
    })
    // 关键字
    getKeyWordsList()
    .then(res => {
      this.keywordArr = res.data;
    })
  },
  methods: {
    resetTemp() {
      this.ruleForm =  {
        compCode: '',
        itemCode: '',
        paramType: '',
        items: [],
        remark: ''
      };
    },
    changeType(val) {
      if (val === '1') {
        this.isType = '1';
      } else if (val === '2') {
        this.isType = '2';
      } else if (val === '') {
        this.isType = '';
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm(this.$t("loanDetail.cue1"), {
            confirmButtonText: this.$t("btn.confirm"),
            cancelButtonText: this.$t("btn.cancel"),
            type: "warning",
            center: true
          })
            .then(() => {
              this.ruleForm.id = this.currentId;
              this.ruleForm.itemCode = this.currentObj.itemCode;
              updateDataChannel(this.ruleForm).then(res => {
                if (res.status === 200) {
                  this.dialogFormVisible = false;
                  this.getList();
                  this.$notify({
                    title: this.$t("user.success"),
                    message: this.$t("user.crtsuccess"),
                    type: "success",
                    duration: 2000
                  });
                } else {
                  this.$message({
                    showClose: true,
                    type: "warning",
                    message: res.message
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                showClose: true,
                type: "info",
                message: this.$t("loanDetail.cue3")
              });
            });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    // 点击编辑按钮
    handleUpdate(row) {
      this.currentId = row.id;
      let obj = {};
      obj.id = this.currentId;
      getDataChannelDetail(obj)
      .then(res => {
        if (res.status === 200) {
          if (res.data.paramType === '1') {
            this.isType = '1';
          } else if (res.data.paramType === '2') {
            this.isType = '2';
          } else if (res.data.paramType === '') {
            this.isType = '';
          }
          this.currentObj = res.data;
          this.ruleForm = Object.assign({}, res.data);
          this.ruleForm.itemCode = row.itemCode;
          this.dialogFormVisible = true;
        }
      })
    },
    changeStatus(row) {
      let obj = {};
      obj.id = row.id;
      obj.enable = row.modelStatus;
      enableDataChannel(obj)
      .then(res => {
        if (res.status === 200) {
          this.$message({
            showClose: true,
            type: "success",
            message: this.$t("approvalDispatch.opersuccess")
          });
          this.getList();
        } else {
          this.$message({
            showClose: true,
            type: "warning",
            message: res.message
          });
          this.getList();
        }
      })
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveModelManagerPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      getModelShowPage(this.listQuery).then(res => {
        res.data.rows.forEach((item, index) => {
          item.index = index + (this.listQuery.page - 1) * this.listQuery.limit + 1
          this.appChannel.forEach(item2 => {
            if (item.channel === item2.channel) {
              item.channel = item2.org_name;
            }
          })
        })
        this.expDis = false;
        this.listLoading = false;
        this.tableData = res.data.rows;
        this.total = Number(res.data.total);
      });
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 10;
      this.listQuery.page = 1;
      this.listQuery.keyWords = '';
      this.listQuery.channel = this.appChannel[0].channel;
      this.listQuery.modelType = '';
    },
    //选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
</style>